<?php
/**
 * @Author: king
 * @Last Modified by:   king
 * Date: 2018/6/21
 * Time: 16:50
 */

namespace App\Models\Api;
use App\Exceptions\ApiException;
use App\Models\Api\Base;
use Illuminate\Database\Eloquent\Model;
use Cache;

class Goods extends Base
{
    protected $table = 'goods';

    private $acg_gids;
    private $day_gids;
    private $month_gids;
    private $year_gids;
    private $video_gids;
    const GOODS_REC = 'goods_rec';//生时矫正商品缓存键名
    const GOODS_DAY = 'goods_day';//日运商品缓存键名
    const GOODS_MONTH = 'goods_month';//月运商品缓存键名
    const GOODS_YEAR = 'goods_year';//年运商品缓存键名
    const GOODS_MAP = 'goods_acg';//占星地图商品缓存键名
    const GOODS_VIDEOS = 'goods:videos';//视频商品缓存键名
    const CACHE_TIME = 86400*7;//缓存有效期

    public function __construct()
    {
        parent::__construct();
        $this->acg_gids = config('app.acg_id');
        $this->day_gids = config('app.day_id');
        $this->month_gids = config('app.month_id');
        $this->year_gids = config('app.year_id');
        $this->video_gids = config('app.video_id');
    }

    /**
     * 获取教学视频的相关商品信息
     * @param array $map
     * @return array|bool|mixed|\PDOStatement|string|\think\Model
     */
    public function getVideoInfo($map=[])
    {
        $id = $map['id'];
        if (!in_array($id,$this->video_gids))
        {
            $this->error = '当前商品号非视频商品信息';

            return false;
        }

        $map['id'] = [$this->video_gids];

        $data = false;

        if (intval($this->cache_on) >= 1 && Cache::has(self::GOODS_VIDEOS))
        {
            $data = Cache::get(self::GOODS_VIDEOS);
        }
        if ($data === false)
        {
            try
            {
                $data = $this->select('id','name','or_prize','now_prize','discount','extend')
                    ->where('status', config('code.status_normal'))
                    ->whereIn('id',$this->video_gids)
                    ->orderBy('sort', 'DESC')
                    ->get();

                $data = json_decode(json_encode($data, true), true);

                Cache::set(self::GOODS_VIDEOS, $data, self::CACHE_TIME);
            }
            catch (\Exception $e)
            {
                $this->error = '教学视频商品信息获取失败';

                return false;
            }
        }

        foreach ($data as $k => $v)
        {
            $extend = json_decode($v['extend'], true);
            if (isset($extend['goods_des']) && $extend['goods_des'])
            {
                $data[$k]['goods_des'] = $extend['goods_des'];
            } else
            {
                $data[$k]['goods_des'] = '';
            }
            unset($data[$k]['extend']);
        }
        return $data;
    }

    /**
     * 获取生时校正商品
     * @param array $map
     * @return array|false|mixed|\PDOStatement|string|\think\Model
     */
    public function getGoodsRec($map = [])
    {
        $id = $map['id'];
        if ($id != 1)
        {
            $this->error = '当前商品号非生时矫正商品信息';

            return false;
        }

        $data = false;

        if (intval($this->cache_on) >= 1 && Cache::has(self::GOODS_REC))
        {
            $data = Cache::get(self::GOODS_REC);
        }

        if ($data === false)
        {
            try
            {
                $data = $this
                    ->select('id','name','or_prize','now_prize')
                    ->where('status', config('code.status_normal'))
                    ->firstOrFail($map['id'])->toArray();

                Cache::set(self::GOODS_REC, $data, self::CACHE_TIME);
            }
            catch (\Exception $e)
            {
                $this->error = '获取生时矫正商品信息失败';

                return false;
            }
        }

        return $data;
    }

    /**
     * 获取占星地图商品
     * @param array $map
     * @return bool|false|mixed|\PDOStatement|string|\think\Collection
     */
    public function getGoodsForACG($map = [])
    {

        $id = $map['id'];
        if (!in_array($id,$this->acg_gids))
        {
            $this->error = '当前商品号非占星地图商品信息';

            return false;
        }
        $map['id'] = ['in', $this->acg_gids];

        $data = false;

        if (intval($this->cache_on) >= 1 && Cache::has(self::GOODS_MAP))
        {
            $data = Cache::get(self::GOODS_MAP);
        }

        if ($data === false)
        {
            try
            {
                $data = $this->select('id','name','or_prize','now_prize','discount')
                    ->where('status', config('code.status_normal'))
                    ->whereIn('id',$this->acg_gids)
                    ->orderBy('sort', 'DESC')
                    ->get();

                $data = json_decode(json_encode($data, true), true);

                Cache::set(self::GOODS_MAP, $data, self::CACHE_TIME);
            }
            catch (\Exception $e)
            {
                $this->error = '获取占星地图商品信息失败';
                return false;
            }

        }
        return $data;
    }

    /**
     * 获取日运商品
     * @param array $map
     * @return bool|false|mixed|\PDOStatement|string|\think\Collection
     */
    public function getGoodsForDay($map = [])
    {
        $id = $map['id'];
        if (!in_array($id,$this->day_gids))
        {
            $this->error = '当前商品号非日运商品信息';

            return false;
        }

        $map['id'] = [$this->day_gids];

        $data = false;

        if (intval($this->cache_on) >= 1 && Cache::has(self::GOODS_DAY))
        {
            $data = Cache::get(self::GOODS_DAY);
        }

        if ($data === false)
        {
            try
            {
                $data = $this->select('id','name','or_prize','now_prize','discount','expire_time','extend')
                    ->where('status', config('code.status_normal'))
                    ->whereIn('id',$this->day_gids)
                    ->orderBy('sort', 'DESC')
                    ->get();
               
                $data = json_decode(json_encode($data, true), true);

                Cache::set(self::GOODS_DAY, $data, self::CACHE_TIME);
            }
            catch (\Exception $e)
            {
                $this->error = '日运商品信息获取失败';
                return false;
            }
        }

        if (!empty($data)) {
            foreach ($data as $k => $v) {
                $extend = json_decode($v['extend'], true);
                if (isset($extend['goods_des']) && $extend['goods_des']) {
                    $data[$k]['goods_des'] = $extend['goods_des'];
                } else {
                    $data[$k]['goods_des'] = '';
                }
                unset($data[$k]['extend']);
            }
        }

        return $data;
    }

    /**
     * 获取月运商品信息
     * @param array $map
     * @return array|bool|mixed|\PDOStatement|string|\think\Model
     */
    public function getGoodsForMonth($map = [])
    {
        $id = $map['id'];
        if (!in_array($id,$this->month_gids))
        {
            $this->error = '当前商品号非月运商品信息';

            return false;
        }

        $map['id'] = [$this->month_gids];

        $data = false;

        if (intval($this->cache_on) >= 1 && Cache::has(self::GOODS_MONTH))
        {
            $data = Cache::get(self::GOODS_MONTH);
        }
        if ($data === false)
        {
            try
            {
                $data = $this->select('id','name','or_prize','now_prize','discount','expire_time','extend')
                    ->where('status', config('code.status_normal'))
                    ->whereIn('id',$this->month_gids)
                    ->orderBy('sort', 'DESC')
                    ->get();

                $data = json_decode(json_encode($data, true), true);

                Cache::set(self::GOODS_MONTH, $data, self::CACHE_TIME);
            }
            catch (\Exception $e)
            {
                $this->error = '月运商品信息获取失败';
                return false;
            }
        }

        if (!empty($data)) {
            foreach ($data as $k => $v) {
                $extend = json_decode($v['extend'], true);
                if (isset($extend['goods_des']) && $extend['goods_des']) {
                    $data[$k]['goods_des'] = $extend['goods_des'];
                } else {
                    $data[$k]['goods_des'] = '';
                }
                unset($data[$k]['extend']);
            }
        }
        return $data;
    }

    /**
     * 获取年运商品信息
     * @param array $map
     * @return array|bool|mixed|\PDOStatement|string|\think\Model
     */
    public function getGoodsForYear($map = [])
    {
        $id = $map['id'];
        if (!in_array($id,$this->year_gids))
        {
            $this->error = '当前商品号非年运商品信息';

            return false;
        }

        $map['id'] = ['in', $this->year_gids];

        $data = false;

        if (intval($this->cache_on) >= 1 && Cache::has(self::GOODS_YEAR))
        {
            $data = Cache::get(self::GOODS_YEAR);
        }
        if ($data === false)
        {
            try
            {
                $data = $this->select('id','name','or_prize','now_prize','discount','expire_time','extend')
                    ->where('status', config('code.status_normal'))
                    ->whereIn('id',$this->year_gids)
                    ->orderBy('sort', 'DESC')
                    ->get();

                $data = json_decode(json_encode($data, true), true);

                Cache::set(self::GOODS_YEAR, $data, self::CACHE_TIME);
            }
            catch (\Exception $e)
            {
                $this->error = '年运商品信息获取失败';
                return false;
            }
        }

        if (!empty($data)) {
            foreach ($data as $k => $v) {
                $extend = json_decode($v['extend'], true);
                if (isset($extend['goods_des']) && $extend['goods_des']) {
                    $data[$k]['goods_des'] = $extend['goods_des'];
                } else {
                    $data[$k]['goods_des'] = '';
                }
                unset($data[$k]['extend']);
            }
        }
        return $data;
    }

}
